<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESResponse extends PEAR
{
    var $responseText;
    
    function sendHTTPHeader($headerCode, $params = null)
    {
        $params = (!$params || !is_array($params)) ? array() : $params;
        switch ($headerCode) {
            case 'REDIRECT':
                if (array_key_exists('LOC', $params)) {
                    $string = 'Location: '.$params['LOC'];
                } else {
                    $this->raiseESError($GLOBALS['ES']['LANG']['SYSERROR']['NOREDIRECTURL'], 'log');
                }
                break;
            case 'JPEG':
                $string = 'Content-type: image/jpeg';
                break;
            case 'GIF':
                $string = 'Content-type: image/gif';
                break;
            case '404':
                $string = 'HTTP/1.0 404 Not Found';
                break;
        }
        header($string);
        return;
    }
    
    function raiseESError($message, $processType)
    {
        $this->responseText = $message;
        $this->raiseError($this->responseText, null, PEAR_ERROR_CALLBACK, array($this, $processType.'Error'));
        return;
    }
    
    function logError()
    {
        /*$entry = date('F j, Y, g:i a').' - ('.ES_BASE_URL.') - '.$this->responseText.'
';
        $noPermMessage = sprintf($GLOBALS['ES']['LANG']['SYSERROR']['LOGNOWRITE'], $GLOBALS['ES']['LOCALLOGFILE']);
        if (is_writable($_SERVER['DOCUMENT_ROOT'].'/../inc/local.log') || ESSystem::chmod($_SERVER['DOCUMENT_ROOT'].'/../inc')) {
            if (!$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/../inc/local.log', 'a')) {
                $this->raiseESError($noPermMessage.$entry, 'email');
            }
            if (fwrite($handle, $entry) === FALSE) {
                $this->raiseESError($noPermMessage.$entry, 'email');
            }
            fclose($handle);
        } else {
            $this->raiseESError($noPermMessage.$entry, 'email');
        }*/
        $log =& Log::factory('file', $_SERVER['DOCUMENT_ROOT'].'/../inc/local.log', ES_BASE_URL);
        $log->log($this->responseText);
        return;
    }
    
    function emailError()
    {
        //echo $this->responseText;
    }
    
    function displayError()
    {
        global $ESDisplay;
        $ESDisplay->assign('RESPONSE_MSG', $this->responseText);
        return;
    }
    
    function dieOnError()
    {
        global $ESDisplay;
        $this->displayError();
		$ESPAGEVARS = array(
		                   'DESCRIPTION' => '',
						   'KEYWORDS'    => '',
						   'PAGETITLE'   => 'System Error'
		                   );
		$ESDisplay->assign('ESPAGEVARS', $ESPAGEVARS);
        $ESDisplay->display('error.tpl');    
        die();
    }
    
    function coreError()
    {
        global $ESMail;
        $mailBody = date('F j, Y, g:i a').' - ('.ES_BASE_URL.') - '.$this->responseText.'\\r\\n';
        $headers['From']    = $GLOBALS['ES']['SUPPORTEMAIL'];
        $headers['To']      = $GLOBALS['ES']['SUPPORTEMAIL'];
        $headers['Subject'] = 'Client configuration container missing/permission denied';
        $ESMail->send($GLOBALS['ES']['SUPPORTEMAIL'], $headers, $mailBody);
        $this->responseText = $GLOBALS['ES']['LANG']['SYSERROR']['SYSDOWNMSG'];
        $this->dieOnError();
    }
}

?>